/****************************************************************************
 * Copyright (c) 2009 IBM, and others.
 *
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * Contributors:
 *   IBM Corporation - initial API and implementation
 *
 * SPDX-License-Identifier: EPL-2.0
 *****************************************************************************/
package org.eclipse.ecf.internal.tests.filetransfer.httpserver;

import java.io.IOException;

import org.apache.commons.httpclient.server.SimpleHttpServer;

public class SimpleServer implements ITestServer {
    private SimpleHttpServer server = null;

    public SimpleServer(String testName) throws IOException {
    	this(testName, testName);
    }
    	
    public SimpleServer(String serverName, String testName)
        throws IOException {
        this.server = new SimpleHttpServer(serverName);
        this.server.setTestname(testName);
    }
    
	public void shutdown() {
		server.destroy();
		server = null;
	}
    
    public SimpleHttpServer getSimpleHttpServer() {
    	return server;
    }
    
    public Object getAdapter(Class adapter) {
    	if (adapter.isInstance(this)) {
    		return this;
    	}
    	if (adapter.isInstance(getSimpleHttpServer())) {
    		return getSimpleHttpServer();
    	}
    	return null;
    }
    
	public String getServerHost() {
		return "localhost";
	}

	public int getServerPort() {
		return server.getLocalPort();
	}
	
	public String getServerURL() {
        String url = "http://" + getServerHost() + ":" + getServerPort();
        return url;
	}

}
